package com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.guide;

import java.util.List;

import com.google.code.irouter.portal.client.AppConstants;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.guide.source.Command;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.guide.source.command.Tag;
import com.google.code.irouter.portal.client.rpc.guide.GuideResult;
import com.google.code.irouter.portal.client.rpc.guide.GuideService;
import com.google.code.irouter.portal.client.rpc.guide.GuideServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Ext;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;

public final class Source extends GridPanel {
	
	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);
	
	private final GuideServiceAsync guideService = GWT.create(GuideService.class);
	
	public static final String GROUP = "Guide";
	
	public Source() {
		super();
		this.setId(Source.class.getName());
		
		this.setWidth(300);
		this.setEnableDragDrop(true);
		this.setDdGroup(GROUP);
		this.setBottomToolbar(new Command());
		ColumnConfig[] columns = {
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_relation_browser_editor_detail_guide_source_column_name(),"name", 100, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_relation_browser_editor_detail_guide_source_column_detector(),"detector", 200, true)
		};
		ColumnModel columnModel = new ColumnModel(columns);
		this.setColumnModel(columnModel);
		Object[][] guides = new Object[0][0];
		Store store = new SimpleStore(new String[] { "name", "detector", "resource", "fixedLink", "key" }, guides);
		store.load();
		this.setStore(store);
	}
	
	public void refresh() {
		disable();
		Tag tag = (Tag) Ext.getCmp(Tag.class.getName());
		guideService.load(
				tag.getText(),
				new AsyncCallback<List<GuideResult>>() {
					public void onFailure(Throwable caught) {
					// TODO
						enable();
					}

					public void onSuccess(List<GuideResult> guideResultList) {
						Object[][] guideArray = new Object[guideResultList.size()][5];
						int index = 0;
						for (GuideResult guideResult : guideResultList) {
							Object[] guideItem = new Object[5];
							guideItem[0] = guideResult.getName();
							guideItem[1] = guideResult.getDetector();
							guideItem[2] = guideResult.getResource();
							guideItem[3] = guideResult.getFixedLink();
							guideItem[4] = String.valueOf(guideResult.getId());
							guideArray[index] = guideItem;
							index++;
						}
						Store store = new SimpleStore(
								new String[] { 
										"name",
										"detector",										
										"resource", 
										"fixedLink",
										"key"
								}, 
								guideArray
						);
						store.load();
						reconfigure(store, getColumnModel());
						enable();
					}
				}
		);
	}
	
	public void reset() {
		Object[][] guides = new Object[0][0];
		Store store = new SimpleStore(new String[] { "name", "detector", "resource", "fixedLink", "key" }, guides);
		store.load();
		this.reconfigure(store, this.getColumnModel());
		
		((Tag) Ext.getCmp(Tag.class.getName())).setValue("");
	}

}
